package sistemadeproyectos;

import java.sql.*;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;

public class SeleccionarEquipo extends javax.swing.JFrame {

    private Usuario usuarioActivo = new Usuario();
    
    public void setUsuario(Usuario elUsuario){
        this.usuarioActivo = elUsuario;
    }
    
    /** Creates new form SeleccionarEquipo */
    public SeleccionarEquipo() {
        initComponents();
        this.getRootPane().setDefaultButton(jbtnAcceder);
        jpwfClave.requestFocus();
    }
    
    public void actualizar(){
        DefaultComboBoxModel modelo = new DefaultComboBoxModel();
        jcboEquipo.setModel(modelo);
        String id;
        try{
            BDAdmin motor = new BDAdmin("SELECT EQUIPO FROM PERTENECE WHERE USUARIO = '" + this.usuarioActivo.getUsuario() + "'");
            ResultSet rs = motor.consulta();
            while (rs.next()) {
                id = rs.getString("EQUIPO");
                modelo.addElement(id);
            }  
            motor.cerrar();
        }
        catch(SQLException sqle){
            sqle.printStackTrace();
        }
    }
        
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jcboEquipo = new javax.swing.JComboBox();
        jbtnAcceder = new javax.swing.JButton();
        jbtnSalir = new javax.swing.JButton();
        jpwfClave = new javax.swing.JPasswordField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(sistemadeproyectos.SistemadeproyectosApp.class).getContext().getResourceMap(SeleccionarEquipo.class);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setName("Form"); // NOI18N
        setResizable(false);

        jLabel1.setFont(resourceMap.getFont("jLabel1.font")); // NOI18N
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jLabel2.setFont(resourceMap.getFont("jLabel2.font")); // NOI18N
        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        jLabel3.setFont(resourceMap.getFont("jLabel3.font")); // NOI18N
        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        jcboEquipo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        jcboEquipo.setName("jcboEquipo"); // NOI18N

        jbtnAcceder.setText(resourceMap.getString("jbtnAcceder.text")); // NOI18N
        jbtnAcceder.setName("jbtnAcceder"); // NOI18N
        jbtnAcceder.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnAccederActionPerformed(evt);
            }
        });

        jbtnSalir.setText(resourceMap.getString("jbtnSalir.text")); // NOI18N
        jbtnSalir.setName("jbtnSalir"); // NOI18N
        jbtnSalir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnSalirActionPerformed(evt);
            }
        });

        jpwfClave.setText(resourceMap.getString("jpwfClave.text")); // NOI18N
        jpwfClave.setName("jpwfClave"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(77, 77, 77)
                .addComponent(jbtnAcceder)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 92, Short.MAX_VALUE)
                .addComponent(jbtnSalir)
                .addGap(107, 107, 107))
            .addGroup(layout.createSequentialGroup()
                .addGap(26, 26, 26)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3)
                    .addComponent(jLabel2))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jpwfClave)
                    .addComponent(jcboEquipo, 0, 207, Short.MAX_VALUE))
                .addContainerGap(22, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addGap(44, 44, 44)
                .addComponent(jLabel1)
                .addContainerGap(49, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addComponent(jLabel1)
                .addGap(26, 26, 26)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jcboEquipo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(27, 27, 27)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jpwfClave, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jbtnAcceder)
                    .addComponent(jbtnSalir))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jbtnAccederActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnAccederActionPerformed
        // TODO add your handling code here:
        String elEquipo = (String)jcboEquipo.getSelectedItem();
        String clave = this.jpwfClave.getText();
        try{
            BDAdmin motor = new BDAdmin("SELECT * FROM EQUIPO WHERE ID_EQUIPO = '"
              + elEquipo + "' AND CLAVE_ACCESO = '" + clave + "'");
            ResultSet rs = motor.consulta();
            if(rs.next() == false) {
                JOptionPane.showMessageDialog(this, "Pase de acceso no valido", 
                    "Clave errada", JOptionPane.ERROR_MESSAGE);
            }
            else{
                MenuPrincipal elMenu = new MenuPrincipal();
                elMenu.setUsuario(this.usuarioActivo);
                Equipo suEquipo = new Equipo();
                suEquipo.setIdEquipo(elEquipo);
                suEquipo.setClaveAcceso(clave);
                elMenu.setEquipo(suEquipo);
                elMenu.setVisible(true);
                elMenu.info();
                this.dispose();
            }
            motor.cerrar();
        }
        catch(SQLException sqle){
            sqle.printStackTrace();
        }
    }//GEN-LAST:event_jbtnAccederActionPerformed

    private void jbtnSalirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnSalirActionPerformed
        // TODO add your handling code here:
        System.exit(0);
    }//GEN-LAST:event_jbtnSalirActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new SeleccionarEquipo().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JButton jbtnAcceder;
    private javax.swing.JButton jbtnSalir;
    private javax.swing.JComboBox jcboEquipo;
    private javax.swing.JPasswordField jpwfClave;
    // End of variables declaration//GEN-END:variables
}